package com.xing.ui.expandableRecyclerView

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.constraintlayout.motion.widget.TransitionBuilder
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet

class ExpandableView : MotionLayout {
//    val mScene: MotionScene by lazy { MotionScene(this) }

    var barTransition: MotionScene.Transition? = null

    val GROUP_ID by lazy { View.generateViewId() }
    val CHILD_ROOT_ID by lazy { View.generateViewId() }

    var groupView: View? = null
    var childRoot: View? = null

    var isExpand = true

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {

        //1 step，开始添加group标题
        groupView = TextView(context).apply {
            id = GROUP_ID
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                topToTop = ConstraintSet.PARENT_ID
                leftToLeft = ConstraintSet.PARENT_ID
                rightToRight = ConstraintSet.PARENT_ID
            }
            text = "Group"
            textSize = 50f
            setTextColor(Color.BLACK)

            addView(this)
            setOnClickListener(this@ExpandableView::onClick)
        }

        //2 step， 开始创建子Item的容器
        childRoot = LinearLayout(context).apply {
            id = CHILD_ROOT_ID
            orientation = LinearLayout.VERTICAL
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                topToBottom = GROUP_ID
                leftToLeft = ConstraintSet.PARENT_ID
                rightToRight = ConstraintSet.PARENT_ID
            }

            for (i in 0 until 3) {
                addView(TextView(context).apply {
                    layoutParams = LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                    )
                    text = "child $i"
                    textSize = 30f
                    setTextColor(Color.BLACK)
                })
            }
        }
        addView(childRoot)

        post {
            val scene = MotionScene(this)
            barTransition = createTransition(scene)

            /**
             * The order matters here.
             * [MotionScene.addTransition] adds the transition to the scene while
             * [MotionScene.setTransition] sets the transition to be the current transition.
             */
            scene.addTransition(barTransition)
            scene.setTransition(barTransition)
            setScene(scene)
            setTransition(barTransition!!.startConstraintSetId, barTransition!!.endConstraintSetId)
        }
    }

    /**
     * Create a basic transition programmatically.
     */
    private fun createTransition(scene: MotionScene): MotionScene.Transition {
        val startSetId = View.generateViewId()
        val startSet = ConstraintSet().apply {
            clone(this@ExpandableView)
            constrainHeight(CHILD_ROOT_ID, childRoot!!.measuredHeight)
            constrainWidth(CHILD_ROOT_ID, childRoot!!.measuredWidth)
        }
        val endSetId = View.generateViewId()
        val endSet = ConstraintSet().apply {
            clone(this@ExpandableView)
            constrainHeight(CHILD_ROOT_ID, 0)
            constrainWidth(CHILD_ROOT_ID, 0)
        }
        val transitionId = View.generateViewId()
        return TransitionBuilder.buildTransition(
            scene,
            transitionId,
            startSetId, startSet,
            endSetId, endSet)
    }

    fun onClick(view: View) {

        if (isExpand) {
            transitionToEnd()
        } else {
            transitionToStart()
        }
        isExpand = !isExpand
    }

}